/**
* @desc:  人员管理-同步名单-同步冲突
* @author qiankui
* @date qiankui 2023-9-13 09:30:28
*/
<template>
    <el-dialog append-to-body="" :close-on-click-modal="false" :visible="syncConflictFlag" class="zdy-dialog" width="70%"
               :title="$t('sync1')" :before-close="handleCloseDialog">
        <el-alert
                type="warning"
                :closable="false"
                show-icon>

            <template slot='title'>
                <div class="iconSize">
                    <!--有人员编号（工号）重复，请在表格最后一列选择处理。-->
                    {{this.i18n.t('sync2')}}
                    <el-tooltip :content="$t('sync3')" placement="top">
                        <el-link style="vertical-align: top;text-decoration: underline" type="primary">
                            <!--合并-->
                            {{this.i18n.t('sync5')}}
                            <i class="icon iconfont-KUI icon-tiaozhuan2 el-icon--right" style="vertical-align: bottom"></i>
                        </el-link>

                    </el-tooltip>

                    <el-tooltip style="margin-left: 10px" :content="$t('sync4')" placement="top">
                        <el-link style="vertical-align: top;text-decoration: underline" type="primary">
                            <!--新增-->
                            {{this.i18n.t('sync6')}}
                            <i class="icon iconfont-KUI icon-tiaozhuan2 el-icon--right" style="vertical-align: bottom;"></i>
                        </el-link>
                    </el-tooltip>

                    </div>
            </template>

        </el-alert>
        <br>
        <el-table
                :data="tableData"
                height="300px"
                style="width: 100%">

            <!--平台内的人员-->
            <el-table-column :label="$t('sync7')">
                <el-table-column
                        prop="province"
                        label="ID"
                        :show-overflow-tooltip="true"
                        >
                    <template slot-scope="scope">
                        {{scope.row.platPerson.personnelCode}}
                    </template>
                </el-table-column>
                <!--编号（工号）-->
                <el-table-column
                        prop="city"
                        :show-overflow-tooltip="true"
                        :label="$t('Number (job number)')"
                        >
                    <template slot-scope="scope">
                        {{scope.row.platPerson.jobNumber}}
                    </template>
                </el-table-column>
                <el-table-column
                        prop="address"
                        :label="$t('name')"
                        :show-overflow-tooltip="true"
                        >
                    <template slot-scope="scope">
                        {{scope.row.platPerson.name}}
                    </template>
                </el-table-column>
            </el-table-column>
            <!--设备内的人员-->
            <el-table-column :label="$t('sync8')">
                <!--"设备编号"-->
                <el-table-column
                        prop="province"
                        :label="$t('Equipment number')"
                        :show-overflow-tooltip="true"
                >
                    <template slot-scope="scope">
                        {{scope.row.devicePerson.deviceCode}}
                    </template>
                </el-table-column>
                <el-table-column
                        prop="city"
                        label="ID"
                        :show-overflow-tooltip="true"
                >
                    <template slot-scope="scope">
                        {{scope.row.devicePerson.personnelCode}}
                    </template>
                </el-table-column>
                <el-table-column
                        prop="address"
                        :label="$t('Number (job number)')"
                        :show-overflow-tooltip="true"
                >
                    <template slot-scope="scope">
                        {{scope.row.devicePerson.jobNumber}}
                    </template>
                </el-table-column>
                <el-table-column
                        prop="address"
                        :label="$t('name')"
                        :show-overflow-tooltip="true"
                >
                    <template slot-scope="scope">
                        {{scope.row.devicePerson.name}}
                    </template>
                </el-table-column>
                <el-table-column :label="$t('operation')">
                    <template slot-scope="scope">

                        <!--确定要合并吗？-->
                        <el-popconfirm
                                :confirm-button-text="$t('OK')"
                                :cancel-button-text="$t('Cancel')"
                                @confirm="mergeFn(scope.row)"
                                :title="$t('sync9')"
                        >
                            <!--{{$t('sync5')}}-->
                            <!--合并-->
                            <el-link :disabled="allLoading" type="primary" slot="reference" style="margin-right: 15px">{{$t('sync5')}}</el-link>
                        </el-popconfirm>
                        <!--确定要新增吗？-->
                        <el-popconfirm
                                :confirm-button-text="$t('OK')"
                                :cancel-button-text="$t('Cancel')"
                                @confirm="addFn(scope.row)"
                                :title="$t('sync10')"
                        >
                            <!--新增-->
                            <el-link :disabled="allLoading" type="success" slot="reference">{{$t('sync6')}}</el-link>
                        </el-popconfirm>

                    </template>
                </el-table-column>
            </el-table-column>
        </el-table>
        <div v-if="tableData.length" style="margin-top: 20px">
            <!--确定要全部合并吗？-->
            <el-popconfirm
                    :confirm-button-text="$t('OK')"
                    :cancel-button-text="$t('Cancel')"
                    @confirm="bulkOperation(2)"
                    :title="$t('sync11')"
            >
                <!--全部合并-->
                <el-button :loading="allLoading" slot="reference" size="small" icon="icon iconfont-KUI icon-hebing1" style="margin-right: 15px">{{this.i18n.t('sync13')}}</el-button>
            </el-popconfirm>
            <!--确定要全部新增吗？-->
            <el-popconfirm
                    :confirm-button-text="$t('OK')"
                    :cancel-button-text="$t('Cancel')"
                    @confirm="bulkOperation(1)"
                    :title="$t('sync12')"
            >
                <!--全部新增-->
                <el-button :loading="allLoading" slot="reference" icon="icon iconfont-KUI icon-gsg-piliangxinzengrenyuan" size="small">{{this.i18n.t('sync14')}}</el-button>
            </el-popconfirm>

            <!--<el-button size="small" @click=""></el-button>-->
        </div>
    </el-dialog>
</template>

<script>
    import { getGuid} from "@/assets/common/js/global";
    import {
        personnelinfoDealpersonimportconfilcts
    } from "@/api/pcmj/zzxx";
    export default {
        name: "syncConflict",
        props: {
            syncConflictFlag: {
                type: Boolean,
                default: false
            },
            conflictTableRow:{
                type:Array,
                default:[]
            }
        },
        data() {
            return {
                allLoading:false,
                keyName:'popupEnable',
                form:{
                    popupEnable:0
                },
                currentRow:[],
                tableData: [],
                // 是否是通过底部工具栏按钮操作
                handelToolButtons:false
            }
        },
        mounted() {
            this.tableData=JSON.parse(JSON.stringify(this.conflictTableRow))
        },
        methods: {
            personnelinfoDealpersonimportconfilcts(params,type){
                this.allLoading=true
                if(Array.isArray(params)){
                    params.map(item=>{
                        item.operate=type
                    })
                }else{
                    params.operate=type
                    params=[params]
                }
                // var data=Array.isArray(params)?params:[params]
                personnelinfoDealpersonimportconfilcts(params).then((res) => {
                    if (res.code == 200) {
                        // 操作成功
                        this.W.$tips("success", this.i18n.t('Operation successful'));
                        if(this.tableData.length){
                            if(!this.handelToolButtons){
                                // 表格内有数据时，删除刚才操作的那一行数据
                                this.tableData.map((item,index)=>{
                                    if (item.guid==this.currentRow.guid){
                                        this.tableData.splice(index,1)
                                    }
                                })
                            }
                        }
                        // 全部新增或全部合并后，清空表格数据，不清空的话，表格关不掉
                        if (this.handelToolButtons){
                            this.tableData=[]
                        }

                    }
                }).catch().finally(()=>{
                    this.allLoading=false
                })
            },
            // 1全部新增，,2全部合并，
            bulkOperation(type){
                // 通过底部批量按钮操作
                this.handelToolButtons=true
                if(type==1){
                    this.personnelinfoDealpersonimportconfilcts(this.tableData,1)
                }else if(type==2){
                    this.personnelinfoDealpersonimportconfilcts(this.tableData,2)
                }
            },
            addFn(row){
                this.handelToolButtons=false
                this.currentRow=row
                var params=JSON.parse(JSON.stringify(row))
                // params.operate=1
                this.personnelinfoDealpersonimportconfilcts(params,1)
                console.log('add',row.guid);



            },
            mergeFn(row){
                this.handelToolButtons=false
                this.currentRow=row
                var params=JSON.parse(JSON.stringify(row))
                // params.operate=2
                this.personnelinfoDealpersonimportconfilcts(params,2)
                console.log('合并',row);
            },

            /**
             * @description  关闭弹窗事件
             * @return not
             * @author xiongyi 2022-06-29 16:48:08
             **/
            handleCloseDialog() {
                if(this.tableData.length){
                    // 表格内有数据时不允许关闭
                    // 有冲突项未处理，请在表格最后一列选择对应的操作。
                    this.W.$tips("warning",this.i18n.t('sync15'));
                    return
                }else{
                    // 关闭当前弹窗
                    this.$emit('update:syncConflictFlag',false)
                    // 刷新刚才选中的在线设备下的人员列表
                    this.$parent.$parent.refreshDevListTree()
                }
            }
        }
    }
</script>

<style scoped>

</style>